package org.example;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class MessageConsumer {
    
    // 监听订单创建消息
    @Service
    @RocketMQMessageListener(
        topic = "ORDER_TOPIC",
        consumerGroup = "order-consumer-group"
    )
    public class OrderCreateConsumer implements RocketMQListener<Order> {
        @Override
        public void onMessage(Order order) {
            log.info("收到订单创建消息: 订单ID={}, 订单号={}, 金额={}, 用户={}", 
                    order.getOrderId(), order.getOrderNo(), order.getAmount(), order.getUserId());
            // 这里可以处理订单创建后的业务逻辑，比如库存扣减、发送通知等
        }
    }
    
    // 监听订单支付消息
    @Service
    @RocketMQMessageListener(
        topic = "ORDER_PAY_TOPIC",
        consumerGroup = "order-pay-consumer-group"
    )
    public class OrderPayConsumer implements RocketMQListener<String> {
        @Override
        public void onMessage(String orderNo) {
            log.info("收到订单支付消息: 订单号={}", orderNo);
            // 处理支付成功后的业务逻辑
        }
    }
    
    // 监听订单取消消息
    @Service
    @RocketMQMessageListener(
        topic = "ORDER_CANCEL_TOPIC",
        consumerGroup = "order-cancel-consumer-group"
    )
    public class OrderCancelConsumer implements RocketMQListener<String> {
        @Override
        public void onMessage(String orderNo) {
            log.info("收到订单取消消息: 订单号={}", orderNo);
            // 处理订单取消的业务逻辑
        }
    }
    
    // 监听订单检查消息（延迟消息）
    @Service
    @RocketMQMessageListener(
        topic = "ORDER_CHECK_TOPIC",
        consumerGroup = "order-check-consumer-group"
    )
    public class OrderCheckConsumer implements RocketMQListener<String> {
        @Override
        public void onMessage(String orderNo) {
            log.info("收到订单检查消息: 订单号={}", orderNo);
            // 检查订单状态，如果未支付则自动取消
            // 这里可以实现自动取消逻辑
        }
    }
    
    // 监听普通测试消息
    @Service
    @RocketMQMessageListener(
        topic = "TEST_TOPIC",
        consumerGroup = "test-consumer-group"
    )
    public class TestMessageConsumer implements RocketMQListener<String> {
        @Override
        public void onMessage(String message) {
            log.info("收到测试消息: {}", message);
        }
    }
}